﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="SinaWeibo_Win8AppStore.MainPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SinaWeibo_Win8AppStore"
    xmlns:data="using:SinaWeibo_Win8AppStore.Data"
    xmlns:vm="using:SinaWeibo_Win8AppStore.ViewModel"
    xmlns:common="using:SinaWeibo_Win8AppStore.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Loaded="pageRoot_Loaded_1"
    mc:Ignorable="d">

    <Page.Resources>
        <common:VisibilityConverter x:Key="visibilityConverter"/>
        <common:SourceConverter x:Key="sourceConverter" />
        <common:BooleanToVisibilityConverter x:Key="boolVisibilityConverter" />
    </Page.Resources>

    <!--
        此网格用作定义以下两行的页的根面板:
        * 第 0 行包含后退按钮和页标题
        * 第 1 行包含页布局的其余部分
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- 后退按钮和页标题-->
        
        <ProgressBar IsIndeterminate="True" Height="10" Width="600" Margin="500,0,0,0"
                      DataContext="{Binding FriendsTimeline}"
                      Visibility="{Binding IsLoading, Converter={StaticResource boolVisibilityConverter}}"
                      HorizontalAlignment="Left"/> 
        
        <Grid DataContext="{Binding UserProfile}" 
              d:DataContext="{Binding UserProfile, Source={d:DesignInstance Type=vm:UserViewModel, IsDesignTimeCreatable=True}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>    
            <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
            <StackPanel Orientation="Horizontal" Grid.Column="2" Margin="0,0,50,0" HorizontalAlignment="Right" VerticalAlignment="Center">
                <TextBlock Text="{Binding ScreenName}" VerticalAlignment="Center" FontSize="22" />
                <Image Source="{Binding ProfileImageUrl}" Height="60" Width="60" Margin="20,0,0,0"/>
                <!--
                <TextBlock Text="{Binding FollowersCount}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="22" Margin="50,0,0,0" />
                <TextBlock Text="{Binding FriendsCount}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="22" Margin="50,0,0,0" />
                <TextBlock Text="{Binding StatusesCount}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="22" Margin="50,0,0,0" />
                -->
                <Button x:Name="ShowNewStatusPopup"  Margin="0" Padding="0" VerticalAlignment="Center" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource AddAppBarButtonStyle}" Click="ShowNewStatusPopup_Click"/>
            </StackPanel>
        </Grid>

        <!-- 在大多数视图状态中使用的水平滚动网格-->
        <ListView  Grid.Row="1" Margin="0,0,0,0" Padding="100,0,0,0" ItemsSource="{Binding StatusFriendsTimeline}" 
                  DataContext="{Binding FriendsTimeline}" 
                  SelectionMode="None" x:Name="StatusList"      
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.HorizontalScrollMode="Enabled"                  
                  d:DataContext="{Binding Source={d:DesignInstance Type=vm:FriendTimelineViewModel, IsDesignTimeCreatable=True}}">
            
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0" Width="500" Height="260" Background="Black">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="20" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <StackPanel Orientation="Vertical" Margin="5" Grid.Column="0" Grid.Row="0">
                            <Border BorderThickness="1" Width="64" Height="64" HorizontalAlignment="Left" VerticalAlignment="Top">
                                <Canvas Margin="0">
                                    <Image Source="{Binding User.ProfileImageUrl}" Height="56" Width="56" Canvas.Left="2" Canvas.Top="2"/>
                                    <!-- <Image x:Name="VerifyLogo" Height="26" Width="26" Canvas.Left="40" Canvas.Top="40" Source="/Resources/Images/SinaResource.Verifier.png" Visibility="{Binding User.Verified, Converter={StaticResource visibilityConverter}}" Stretch="UniformToFill"/> -->
                                </Canvas>
                            </Border>
                        </StackPanel>
                        <StackPanel Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Orientation="Vertical" VerticalAlignment="Top" Margin="5" MaxHeight="220">
                            <HyperlinkButton Height="40" Margin="0" Padding="0" HorizontalContentAlignment="Left" FontSize="16" Content="{Binding User.ScreenName}">
                                <HyperlinkButton.Foreground>
                                    <SolidColorBrush Color="AliceBlue"/>
                                </HyperlinkButton.Foreground>
                            </HyperlinkButton>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <RichTextBlock common:StatusConverter.Status="{Binding Text}" FontSize="14" Grid.Column="0"/>
                                <Image Source="{Binding ThumbnailPic}" Stretch="Uniform" Grid.Column="1"
                                   MaxHeight="120" MaxWidth="120" Visibility="{Binding ThumbnailPic, Converter={StaticResource visibilityConverter}}"
                                   Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center" />
                            </Grid>
                            <Path Data="M 7.5 2.5 L 12 10 2.5 10 Z" Margin="5,0,0,0" Visibility="{Binding RetweetedStatus, Converter={StaticResource visibilityConverter}}"
                                  StrokeThickness="0" 
                                  StrokeLineJoin="Round" 
                                  Stroke="Gray" 
                                  Fill="Gray" />
                            <Border Margin="0,0,5,0" Visibility="{Binding RetweetedStatus, Converter={StaticResource visibilityConverter}}" 
                                    CornerRadius="5" >
                                <Border.Background>
                                    <SolidColorBrush Color="Gray"/>
                                </Border.Background>

                                <Grid DataContext="{Binding RetweetedStatus}" >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Orientation="Vertical" Margin="5" Grid.Column="0">
                                        <TextBlock TextWrapping="Wrap" Text="{Binding User.ScreenName}" Foreground="Blue" FontWeight="Bold" FontSize="14" />
                                        <RichTextBlock common:StatusConverter.Status="{Binding Text}" FontSize="13"/>
                                    </StackPanel>
                                    <Image Margin="5" Grid.Column="1"
                                       Stretch="Uniform" 
                                       Source="{Binding ThumbnailPic}" 
                                       Visibility="{Binding ThumbnailPic, Converter={StaticResource visibilityConverter}}" 
                                       HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" 
                                       MaxHeight="80" MaxWidth="80" />
                                </Grid>
                            </Border>
                        </StackPanel>
                        <StackPanel Margin="300,10,10,0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
                            <TextBlock Text="{Binding RepostsCount}" Width="40"></TextBlock>
                            <TextBlock Text="{Binding CommentsCount}"  Width="40"></TextBlock>
                        </StackPanel>
                        <StackPanel Margin="0,230,10,0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
                            <TextBlock Text="{Binding Idstr}" FontSize="10"  />
                            <TextBlock Text="{Binding CreatedAt}" FontSize="10" Margin="20,0,0,0"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical" MaximumRowsOrColumns="2" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

        <Popup x:Name="NewStatusPopup" IsLightDismissEnabled="False"
               IsOpen="False" HorizontalOffset="300" VerticalOffset="300"   >
            <Popup.ChildTransitions>
                <TransitionCollection>
                    <PopupThemeTransition />
                </TransitionCollection>
            </Popup.ChildTransitions>
            <local:EditControl />
        </Popup>

        <!-- 垂直滚动列表仅在对齐后使用-->
        <ListView
            x:Name="itemListView"
            AutomationProperties.AutomationId="ItemListView"
            AutomationProperties.Name="Grouped Items"
            Grid.Row="1"
            Visibility="Collapsed"
            Margin="0,-10,0,0"
            Padding="10,0,0,60"
            ItemsSource="{Binding StatusFriendsTimeline}"
            ItemTemplate="{StaticResource Standard80ItemTemplate}"
            SelectionMode="None"
            IsSwipeEnabled="false"
            IsItemClickEnabled="True"
            ItemClick="ItemView_ItemClick">

            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid Margin="7,7,0,0">
                                <Button
                                    AutomationProperties.Name="Group Title"
                                    Click="Header_Click"
                                    Style="{StaticResource TextPrimaryButtonStyle}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Text}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
                                        <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
                                    </StackPanel>
                                </Button>
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>
        
        <VisualStateManager.VisualStateGroups>
            <!-- 视觉状态反映应用程序的视图状态-->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- 整页采用较窄的 100 像素页边距约定(纵向)-->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="96,137,10,56"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    对齐后，后退按钮和标题的样式将有所不同，并且列表表示形式将被
                    所有视图状态中显示的网格所替代
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemListView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
